import Alert from '@/components/Alert'
import { ruleType } from '@kedacom/react-next'
import { regularType } from '@kedacom/react-next/dist/types/Input/InnerInput'
import { useBasicInfoStore } from './blocks/basicInfo/store'
import { useMeetingParamsStore } from './blocks/meetingParams/store'
import { t } from 'i18next'

export const ruleTests: {
	[key: string]: any[]
} = {
	encryptedKey: [
		{
			regex: /^[A-Za-z0-9_.]*$/,
			msg: t('可输入16个字符，包括英文字母(大小写)、数字、"."、"_"'),
		},
	],
	mobilePhone: [
		{
			regex: /^[0-9]*$/,
			msg: t('输入非法字符'),
		},
		{
			regex: /^.{0,11}$/,
			msg: t('手机号码最多11个字符'),
		},
	],
	password: [
		{
			regex: /^[0-9]*$/,
			msg: t('会议密码存在非法字符，请重新输入'),
		},
	],
	phoneNO: [
		{
			regex: /^[0-9\.\_\-\*\#\s]*$/,
			msg: t('分机号码输入错误'),
		},
		{
			regex: /^.{0,10}$/,
			msg: t('分机号码最多10个字符'),
		},
	],
	rate: [
		{
			required: true,
			msg: t('自定义码率不能为空'),
		},
		{
			numberLimit: [64, 8192],
			msg: t('自定义码率范围为64K~8M'),
		},
	],
	cascadeReturnPara: [
		{
			required: true,
			msg: t('回传带宽不能为空'),
		},
		{
			numberLimit: [1, 1000],
			msg: t('回传带宽范围为：1M~1000M'),
		},
	],
	callInterval: [
		{
			required: true,
			msg: t('呼叫间隔不能为空'),
		},
		{
			numberLimit: [10, 300],
			msg: t('呼叫间隔为10~300秒'),
		},
	],
	callTimes: [
		{
			required: true,
			msg: t('呼叫次数不能为空'),
		},
		{
			numberLimit: [2, 99],
			msg: t('呼叫次数为2~99秒'),
		},
	],
}

export const getRule = (val: string) => {
	let ruleTest = ruleTests[val]
	let obj: ruleType = {}
	let regulars: regularType[] = []
	ruleTest.forEach((rule) => {
		if (rule.required) {
			obj.required = { msg: rule.msg }
		} else if (rule.numberLimit) {
			regulars.push({
				regex: (val: string) => {
					return Number(val) < rule.numberLimit[0] || Number(val) > rule.numberLimit[1] ? true : false
				},
				msg: rule.msg,
			})
		} else if (rule.regex) {
			regulars.push(rule)
		}
	})
	if (regulars.length !== 0) {
		obj.regular = regulars
	}
	return obj
}

export const checkRule = () => {
	const basicData = useBasicInfoStore.getState()
	const paramData = useMeetingParamsStore.getState()
	const data = { ...basicData, ...paramData }
	for (let item in ruleTests) {
		let rules = ruleTests[item]
		let val = (data as any)[item]
		for (let rule of rules) {
			if (item === 'cascadeReturnPara' && (!data.cascadeReturn || data.simpleCascade !== 1)) {
				continue
			}
			if (item === 'encryptedKey' && data.encryptionType !== 2) {
				continue
			}
			if (item === 'callTimes' && (data.callMode === 0 || data.callTimesUnlimited)) {
				continue
			}
			if (item === 'rate' && data._bitrateType !== 0) {
				continue
			}
			if (item === 'rate' && rule.required && val === undefined) {
				//针对自定义码率判空的特别处理
				Alert(rule.msg)
				return false
			}
			if (rule.required && val === '') {
				Alert(rule.msg)
				return false
			} else if (
				rule.numberLimit &&
				(parseInt(val) < rule.numberLimit[0] || parseInt(val) > rule.numberLimit[1])
			) {
				Alert(rule.msg)
				return false
			} else if (rule.regex) {
				if (rule.regex instanceof Function) {
					if (rule.regex(val)) {
						Alert(rule.msg)
						return false
					}
				} else if (!rule.regex.test(val)) {
					Alert(rule.msg)
					return false
				}
			}
		}
	}
	return true
}
